﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Results_Comparer.Properties;
using System.Drawing;
using System.Drawing.Imaging;

namespace Results_Comparer
{
    public class Program
    {
        /// <summary>
        /// Create our outgoing path, and write out our data
        /// </summary>
        /// <param name="OutputDirectory"></param>
        /// <param name="DifferenceXml"></param>
        /// <param name="staticFol"></param>
        private static void WriteOutputFiles(string OutputDirectory, string DifferenceXml)
        {
            //Create our output directories
            foreach (String dir in new string[] { OutputDirectory, OutputDirectory + "\\html", OutputDirectory + "\\data"})
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);

            //Write out our static files
            File.WriteAllText(OutputDirectory + "\\main.html", Resources.main);
            File.WriteAllBytes(OutputDirectory +"\\html\\bullet.gif", GetGIFBytes(Resources.bullet));
            File.WriteAllBytes(OutputDirectory +"\\html\\minus.gif", GetGIFBytes(Resources.minus));
            File.WriteAllBytes(OutputDirectory +"\\html\\plus.gif", GetGIFBytes(Resources.plus));
            File.Copy(DifferenceXml, OutputDirectory + "\\data\\difference.xml", true);
        }

        /// <summary>
        /// Return a byte array for our image
        /// </summary>
        /// <param name="InImage"></param>
        /// <returns></returns>
        private static byte[] GetGIFBytes(Bitmap InImage)
        {
            using (MemoryStream mS = new MemoryStream())
            {
                InImage.Save(mS, ImageFormat.Gif);
                return mS.ToArray();
            }
        }

        private static void Main(string[] args)
        {
            string str;
            string str3;
            string str2 = args[1];
            if (!args[0].EndsWith(@"\"))
            {
                str = args[0] + @"\";
            }
            else
            {
                str = args[0];
            }
            if (!args[2].EndsWith(@"\"))
            {
                str3 = args[2] + @"\";
            }
            else
            {
                str3 = args[2];
            }
            Console.WriteLine(str);
            Console.WriteLine(str2);
            WriteComparisonXml(str, str2);           
        }

        //AlertsViewBuilder builder2 = new AlertsViewBuilder(str3 + "//config//alertItems.xml");


        /// <summary>
        /// Write a comparison XML document
        /// </summary>
        /// <param name="TargetDirectory"></param>
        /// <param name="InputXml"></param>
        public static void WriteComparisonXml(String TargetDirectory, String InputXml)
        {
            WriteOutputFiles(TargetDirectory, InputXml);
            Builder builder = new Builder(InputXml);         
            
            //Write out our content
             ContentFormatter formatter = new ContentFormatter();
            using (StreamWriter sContents = new StreamWriter(Path.Combine(TargetDirectory , "html","contents.html")))
            using (StreamWriter sAdditions = new StreamWriter(Path.Combine(TargetDirectory , "html","adds.html")))
            using (StreamWriter sDeletions = new StreamWriter(Path.Combine(TargetDirectory , "html","dels.html")))
                formatter.format(sContents, sAdditions, sDeletions);

            using (StreamWriter sFind = new StreamWriter(Path.Combine(TargetDirectory, "html","find.html")))
                WriteFindJS(sFind, Resources.find);

            using (StreamWriter sNav = new StreamWriter(Path.Combine(TargetDirectory, "html", "nav.html")))
                 DifferenceViewFormatter.WriteNavJS(sNav, Resources.diffs);

            using (StreamWriter sAlerts = new StreamWriter(Path.Combine(TargetDirectory, "html", "alerts.html")))
            {
                AlertsViewBuilder builder2 = new AlertsViewBuilder(Resources.AlertItems);
                AlertsViewFormatter formatter4 = new AlertsViewFormatter(builder2.Process(Difference.changes));
                formatter4.format(sAlerts);
            }

            using (StreamWriter sPathNav = new StreamWriter(Path.Combine(TargetDirectory, "html", "pathnav.html")))
            {
                HierarchicalViewBuilder builder3 = new HierarchicalViewBuilder();
                HierarchicalViewFormatter formatter5 = new HierarchicalViewFormatter(builder3.Process(Difference.GetAllDifferences()));
                formatter5.format(sPathNav);
            }                
        }

        private static void Print(TreeNode node, string str)
        {
            if (node != null)
            {
                Console.WriteLine(str + node.name);
                List<TreeNode> childs = node.childs;
                foreach (TreeNode node2 in childs)
                {
                    Print(node2, str + "-");
                }
            }
        }


        #region Formatters
        /// <summary>
        /// Create the find JavaScript
        /// </summary>
        /// <param name="f"></param>
        /// <param name="InScript"></param>
        public static void WriteFindJS(StreamWriter f, string InScript)
        {
            f.Write("<html>\n");
            f.Write("<head>\n");
            f.Write("<SCRIPT LANGUAGE=\"JavaScript\">");
            f.Write(InScript);
            f.Write("</SCRIPT>");
            f.Write("</head>\n");
            f.Write("<body>\n");
            f.Write("<form name=\"search\" onSubmit=\"return findInPage(this.str.value);\">  <div align=\"center\"> <p><font size=3> <input name=\"str\"  type=\"text\" size=25 onChange=\"n = 0;\"> </font> <input  type=\"submit\" value=\"Find\"> </p> </div>  </form>");
            f.Write("</body>\n");
            f.Write("</html>\n");
        }

    
        #endregion
    }
}